#import "CountImageCell.h"

@implementation CountImageCell

- (id)init {
  self = [super init];
  if (self != nil) {
    previousCount = -2;
    blueOvalColor = [[NSColor colorWithDeviceRed:154.0/255.0
                                           green:171.0/255.0 
                                            blue:201.0/255.0 
                                           alpha:1.0] retain];
    
    selectedFontAttribute = [[NSDictionary dictionaryWithObjectsAndKeys:
                              [NSFont fontWithName:@"Helvetica-Bold" size:11], NSFontAttributeName,
                              [NSColor colorWithDeviceRed:150.0/255.0 green:161.0/255.0 blue:183.0/255.0
                                                    alpha:1.0], NSForegroundColorAttributeName, nil] retain];
    
    fontAttribute = [[NSDictionary dictionaryWithObjectsAndKeys: [NSFont
                                                                  fontWithName:@"Helvetica-Bold" size:11], 
                      NSFontAttributeName,  
                      [NSColor whiteColor], 
                      NSForegroundColorAttributeName, nil] retain];
    
    [self setImageAlignment:NSImageAlignRight];
  }
  return self;
}

- (void)setObjectValue:(id)aValue {
  if ([aValue isKindOfClass:[NSImage class]]) {
    [super setObjectValue:aValue];
    return; 
  }
  
  int count = [aValue intValue];
  previousCount = count - 1;
  
  if (previousCount != count) {
    if (count == -1) { // a Group colelction don't draw
      [super setObjectValue:nil];
      return;
    }
        
    int additonalSpace;
    int centeringSpace;
    if (count < 10) {
      additonalSpace = 0;
      centeringSpace = 1;
    } else if (count < 100) {
      additonalSpace = 0;
      centeringSpace = -2;
    } else if (count < 1000) {
      additonalSpace = 10;
      centeringSpace = 0;
    } else if (count < 10000) {
      additonalSpace = 16;
      centeringSpace = 0;
    } else {
      additonalSpace = 20;
      centeringSpace = 0;
    }
    
    NSString *numberString;
    if (count > 10000)
      numberString = [NSString stringWithFormat:@"%2.1fk", count / 1000.0f];
    else
      numberString = [NSString stringWithFormat:@"%d", count];
    
    NSSize imageSize = NSMakeSize(24 + additonalSpace, 14);
    NSImage *dragImage = [[NSImage alloc] initWithSize:imageSize];
    
    [dragImage lockFocus];
    
    
    [blueOvalColor set];
    
    NSBezierPath *p = [NSBezierPath
                       bezierPathWithRoundRectInRect:NSMakeRect(0, 0, imageSize.width - 4, 14)
                       radius:7.0];
    [p fill];
    
    [numberString drawInRect:NSMakeRect(6 + centeringSpace, 0, imageSize.width, 14)
              withAttributes:fontAttribute];
    
    [dragImage unlockFocus];
    
    [super setObjectValue:[dragImage autorelease]];
    previousCount = count;
  }
}

@end


@implementation NSBezierPath (CocoaDevCategory)

+ (NSBezierPath*)bezierPathWithRoundRectInRect:(NSRect)aRect
                                        radius:(float)radius {
  NSBezierPath* path = [self bezierPath];
  radius = MIN(radius, 0.5f * MIN(NSWidth(aRect), NSHeight(aRect)));
  NSRect rect = NSInsetRect(aRect, radius, radius);
  [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(rect),
                                                      NSMinY(rect)) 
                                   radius:radius startAngle:180.0
                                 endAngle:270.0];
  [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(rect),
                                                      NSMinY(rect)) 
                                   radius:radius startAngle:270.0
                                 endAngle:360.0];
  [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(rect),
                                                      NSMaxY(rect)) 
                                   radius:radius startAngle:  0.0
                                 endAngle: 90.0];
  [path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(rect),
                                                      NSMaxY(rect)) 
                                   radius:radius startAngle: 90.0
                                 endAngle:180.0];
  [path closePath];
  return path;
}

@end

@implementation MailLikeTextCell 


- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
  NSLog(@"Object Value : %@", [self objectValue]);
  [super drawWithFrame:cellFrame inView:controlView];
}

@end